package com.athuahua.spzx.aspect;

import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.vo.system.SysOperLogVo;
import com.athuahua.spzx.service.AsyncOperLogService;
import com.athuahua.spzx.util.AutoLogUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @Author: huahua
 * @Date: 2024/8/6 0006
 * @Time: 19:21
 * @Description:
 */
@Aspect
@Component
@Slf4j
public class LogAspect {

    @Resource
    private AsyncOperLogService asyncOperLogService;

    //环绕通知
    @Around(value = "@annotation(sysLog)")
    public Object doAroundAdvice(ProceedingJoinPoint joinPoint, AutoLog sysLog){
        String title = sysLog.title();
        int businessType = sysLog.businessType();
        log.info("更新日志记录......");
        //业务方法调用之前封装数据
        SysOperLogVo log = new SysOperLogVo();
        AutoLogUtil.beforeHandleLog(sysLog,joinPoint,log);


        //执行目标方法
        Object result = null;
        try {
            result = joinPoint.proceed();
            AutoLogUtil.afterHandlLog(sysLog,result,log,0 ,null);

        } catch (Throwable e) {
            AutoLogUtil.afterHandlLog(sysLog,result,log,1 ,e.getMessage());
            throw new RuntimeException(e);
        }
        finally{
            //调用service方法添加日志信息
            asyncOperLogService.saveSysOperLog(log);
        }



        return result;
    }
}
